import React from 'react'
import ReactDom from 'react-dom'
import App from './common/App'
import Login from './pages/login/index'
import './common/common.scss'
import { csDel, csGet } from './utils/cookie'
import { getLogger } from './redux/action/common'
//store
import store from './common/store'
import API from './ApiServer/Auth'
/**
 *根据参数渲染页面
 * @param hasToken 有，渲染页面，没有渲染登录界面
 * @param common 有，直接渲染，没有，先请求，在渲染
 */
async function renderPage(hasToken, common) {
  if (hasToken && !common.logger) {
    let res = await API.userLogin()
    await store.dispatch(getLogger(res))
    let comm = store.getState().Common
    if (!comm.logger) {
      csDel('X-AUTH-TOKEN', '/', 'yimifudao.com')
      location.reload()
    }
  }
  //渲染DOM
  ReactDom.render(hasToken ? <App /> : <Login />, document.getElementById('root'))
}
renderPage(csGet('X-AUTH-TOKEN'), store.getState().Common)
